﻿// Copyright (c) 2012, Alexander Endris
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#region Usings

using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using JungleTimer.Windows;
using Microsoft.Win32;

#endregion

namespace JungleTimer.LoL
{
  public class Settings
  {
    #region Public / Attributes

    public bool Announce10Seconds = true;
    public bool Announce1Minute = true;
    public bool Announce30Seconds = true;
    public string ChatBaron = "Baron";
    public string ChatDragon = "Drake";
    public string ChatOurBlue = "Our Blue";
    public string ChatOurRed = "Our Red";
    public string ChatSeparator = "at";
    public string ChatTheirBlue = "Their Blue";
    public string ChatTheirRed = "Their Red";

    public Keys HotkeyBaron = Keys.NumPad1;
    public Keys HotkeyDragon = Keys.NumPad2;
    public Keys HotkeyOurBlue = Keys.NumPad3;
    public Keys HotkeyOurRed = Keys.NumPad4;
    public Keys HotkeyTheirBlue = Keys.NumPad5;
    public Keys HotkeyTheirRed = Keys.NumPad6;
    public string LoLPath = @"C:\Riot Games\League of Legends\";

    public string ManualStart
    {
      get { return ManualStartTime.ToString(); }
      set { ManualStartTime = TimeSpan.Parse(value); }
    }

    public Keys ManualStartStopHotkey = Keys.NumPad0;

    [XmlIgnore] public TimeSpan ManualStartTime = new TimeSpan(0, 0, 0, 15);
    public bool ShowTimerOverlay = true;

    public bool SpeechEnabled
    {
      get { return Announce10Seconds || Announce1Minute || Announce30Seconds; }
    }

    public bool WriteToChat = true;

    #endregion

    #region Public / Methods

    public static Settings LoadSettings(string path)
    {
      Settings settings;
      if (File.Exists(path))
      {
        using (FileStream str = new FileStream(path, FileMode.Open))
        {
          XmlSerializer serializer = new XmlSerializer(typeof (Settings));
          settings = (Settings) serializer.Deserialize(str);
          if (string.IsNullOrEmpty(settings.ChatSeparator))
            settings.ChatSeparator = " ";
        }
      }
      else
      {
        settings = new Settings();

        string lolPath =
          (RegistryHelper.ReadKey(Registry.LocalMachine, "Software\\Riot Games\\RADS", "LocalRootFolder") ??
           RegistryHelper.ReadKey(Registry.LocalMachine, "Software\\Wow6432Node\\Riot Games\\RADS", "LocalRootFolder"));

        if (lolPath != null)
          settings.LoLPath = lolPath.Substring(0, lolPath.IndexOf("RADS"));
        else
        {
          lolPath = RequestLolPath();
          if (lolPath != null)
            settings.LoLPath = lolPath;
        }

        SaveSettings(path, settings);
      }
      return settings;
    }

    public static void SaveSettings(string path, Settings settings)
    {
      using (FileStream str = new FileStream(path, FileMode.Create))
      {
        XmlSerializer serializer = new XmlSerializer(typeof (Settings));
        serializer.Serialize(str, settings);
      }
    }

    #endregion

    #region Private / Methods

    private static string RequestLolPath()
    {
      FolderBrowserDialog fbd = new FolderBrowserDialog();

      fbd.Description =
        "Please specify the league of legends root folder to enable the use of the autostart on game start functionality. Also, please make sure a 'Logs' folder and in there a 'Game - R3d Logs' folder exists.";
      if (fbd.ShowDialog() == DialogResult.OK)
        return fbd.SelectedPath;
      return null;
    }

    #endregion
  }
}